\documentclass[border=10pt,tikz]{standalone}
\usepackage{pgfplots,pgfplotstable,tikz}
\usetikzlibrary{intersections,matrix}
\usepackage{verbatim}
\usepackage{moresize}
\usetikzlibrary{calc}
\usepackage{datatool}
\usetikzlibrary{shapes}
\usetikzlibrary{plotmarks}
\usepackage{csvsimple}
\usepackage{siunitx}
\usepackage{mathtools}
\usepackage{amsmath,amssymb}
\usetikzlibrary{shapes.geometric}
\usepackage{booktabs}
\usepackage{xspace} 
\usepackage{etoolbox}
\usepackage{multirow}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.text}
\usetikzlibrary{decorations.pathmorphing}
\pgfplotsset{compat=1.13}
\usepackage{fp}
\usepgfplotslibrary{fillbetween}


\definecolor{green}{RGB}{68,126,35}
\definecolor{blue}{RGB}{19,30,92}
\definecolor{red}{RGB}{219,51,24}

% Defines "sgn" function
\def\sgn{\mathop{\rm sgn}\nolimits} 
% Defines the expectation function
\def\E{\mathop{\mathrm{E}}\nolimits} 

% Defines a function that ignores the zero on the y- or the x-axis, such that the origin
% is only labeled once. Activate in the axis options by adding the key: "ignore zero=x" or ""ignore zero=y."
\pgfplotsset{ignore zero/.style={%
		#1ticklabel={\ifdim\tick pt=0pt \else\pgfmathprintnumber{\tick}\fi}
	}}
	
	
	
	\begin{document}
		
	% TAKE INPUTS FROM THE FOLLOWING FILE:
	% "global_polynomial_all_specs.xls" --- generated in Stata by the program file: "global_polynomial_all_specs.do" 
	
			\pgfmathsetmacro\pBdi{0.15}
			\pgfmathsetmacro\pIdi{0.41}
			
			%For plotting the MTE(p) line, use values from the spreadsheet "Y_num_FN1_none." 
			\pgfmathsetmacro\MTEslope{-1.32189329694664} %slope of the MTE(p) line (mte_term_2)
			\pgfmathsetmacro\MTEintercept{0.63892031450335} %intercept of the MTE(p) line (mte_term_1)
			
			%For plotting the "E[MTE(p,X)]: age, female, English" line, use values from the spreadsheet "Y_num_FN1_common"
			\pgfmathsetmacro\MTEcommonslope{-1.88146181075521} %slope of the E[MTE(p,X)] line (SMTE_term_2)
			\pgfmathsetmacro\MTEcommonintercept{0.819249428000153} %intercept of the E[MTE(p,X)] line (SMTE_term_1)
			
			%For plotting the "E[MTE(p,X)]: pre-period" line, use values from the spreadsheet "Y_num_FN1_pre_all"
			\pgfmathsetmacro\MTEpreERlope{0.1227642482326} %slope of the E[MTE(p,X)] line (SMTE_term_2)
			\pgfmathsetmacro\MTEpreERintercept{0.263697406329602} %intercept of the E[MTE(p,X)] line (SMTE_term_1)
		
			% MTE(pC) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
			\begin{tikzpicture}[>=stealth]
			% Axis options
			\begin{axis}[					
			width=15cm,					% width of chart
			height=10cm,				% height of chart
			xlabel={$U_D$:  unobserved net cost of treatment},				% x-axis title
			ylabel={Number of ER Visits},					% y-axis title
			ylabel style={rotate=0},  % rotate the y-axis title
			samples=100,  				% sets the resolution for curved lines
			xmin=0,xmax=1,			    % x-axis range
			ymin=-1.25,
			%ymin=1.2*\Dinterceptr,ymax=1.05*\yaxismax,			% y-axis range
			ignore zero=y,			    % ignore the zero on the y-axis (defined in preamble)
			xtick={0,\pBdi,\pIdi,1},	    % only show these x ticks
			xticklabels={0,$p_C=\pBdi$, $p_I=\pIdi$,1}, % label x ticks
			ytick={0,\MTEintercept,\MTEcommonintercept,\MTEpreERintercept},	    % only show these y ticks
			ymax = 1.5,
			legend style={draw=none,cells={align=left}},	% set legend style, specifically no outline
			legend cell align={left},	% align legend content to the left
			legend pos = north east,
			clip=false,					% place nodes outside of plot area
			xticklabel style={
						yshift=-5.5ex,
						name=label,
						append after command={(label.north) edge [solid] ++(0,6ex)},
						/pgf/number format/fixed,
						/pgf/number format/precision=2},
					x label style={at={(axis description cs:0.5,-0.15)},anchor=north}, % ncrease distance between title and x-axis
						yticklabel style={	/pgf/number format/fixed,
						/pgf/number format/precision=2},
					% place some extra y ticks ...
					extra y ticks={0},
					% ... but do not repeat the already existent labels
					% (from the "normal" ticks) ...
					extra y tick labels={0},
					% ... and set the style of the extra ticks to `major'
					extra tick style={
						grid=major,
					},
			 mydot/.style={
			 	circle,
			 	fill=white,
			 	draw,
			 	outer sep=0pt,
			 	inner sep=1.5pt
			 },
			]			
			
			% Add right-hand yticklabel
			\node[anchor=west] () at (axis cs: 1,-0.68) {$-0.68$};
			\node[anchor=west] () at (axis cs: 1,-1.06) {$-1.06$};
			\node[anchor=west] () at (axis cs: 1,0.39) {$\phantom{-}0.39$};
			
			%mte(p)
			\addplot[domain=0:1, line width=1.8pt, red, opacity=1]
				{\MTEintercept+\MTEslope*\x}
				node[pos=0.28,black,pin={[pin edge={black},pin distance=0pt]-90:{\textcolor{black}{\small LATE}}}]{}
				;
			\node[outer sep=0pt,diamond, fill=red,inner sep=1.75pt] (P) at (0.5*\pBdi + 0.5*\pIdi, 0.267122474201187) {};
			
			\addplot[domain=0:1, line width=1.2pt, black!15!red, opacity=0.66]
				{\MTEcommonintercept+\MTEcommonslope*\x};
						
			\addplot[domain=0:1, line width=1.2pt, black!15!red, opacity=0.33]
					{\MTEpreERintercept+\MTEpreERlope*\x};

			%Add Dotted Lines
			\addplot[
				line width=1pt, black, dashed, opacity=100, thin
			] coordinates {(\pBdi,-1.25) (\pBdi,\MTEcommonintercept+\MTEcommonslope*\pBdi)};
			
			\addplot[
				line width=1pt, black, dashed, opacity=100, thin
			] coordinates {(\pIdi,-1.25) (\pIdi,\MTEpreERintercept +\MTEpreERlope*\pIdi)};

			% add legend
			\legend{\small $\text{MTE}(p)$, \small {$\E[\text{MTE}(p, X)]$}: {age, female, English}, \small {$\E[\text{MTE}(p, X)]$}: {pre-period ER visits},,}

			% Put Always Takers, Compliers, and Never Takers on the x-axis
			\draw[draw=none,decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt] (0,-1.25) -- (\pBdi, -1.25) node[pos=0.5, below, yshift = 0pt, align=left] {Always \\ Takers};
			\draw[draw=none,decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt] (\pBdi,-1.25) -- (\pIdi, -1.25) node[pos=0.5, below, yshift = 0pt] {Compliers};
			\draw[draw=none,decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt] (\pIdi,-1.25) -- (1, -1.25) node[pos=0.5, below, yshift = 0pt] {Never Takers};


			\end{axis}
			
		
			\end{tikzpicture}
		
		
	\end{document}
